package com.youyun56.gps.android.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class CloseSystemDialogBroadCast extends BroadcastReceiver {
    private static Boolean      screenNoLock = true;
    private final static long   noticeDelay  = 1000;
    private final static String tag          = CloseSystemDialogBroadCast.class.getName();
    private Thread              delayNoticeThread;
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(tag, intent.getAction());
        screenNoLock = intent.getAction().equals("android.intent.action.SCREEN_ON");
        if (delayNoticeThread != null && delayNoticeThread.isAlive() && !delayNoticeThread.isInterrupted()) {
            delayNoticeThread.interrupt();
        }
        if (screenNoLock) {
            // LocationUploadService.getInstance().noticeActivive(screenNoLock);
        } else {
            delayNoticeThread = null;
            delayNoticeThread = new DelayNoticeThread();
            delayNoticeThread.setDaemon(true);
            delayNoticeThread.start();
        }
    }
    
    public static Boolean getOpen() {
        return screenNoLock;
    }
    
    public static void setOpen(Boolean open) {
        screenNoLock = open;
    }
    
    class DelayNoticeThread extends Thread {
        @Override
        public void run() {
            try {
                Log.i(tag, "start Delay Notice");
                Thread.sleep(noticeDelay);
                if (!screenNoLock) {
                    // LocationUploadService.getInstance().noticeActivive(screenNoLock);
                }
            } catch (InterruptedException e) {
                Log.e(tag, e.getMessage(), e);
            }
        }
    }
    
}
